﻿using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace PwManager
{
    public class StrongBoxCore
    {
        public static IEnumerable<StrongBox> PagerList(StrongBox box)
        {
            var sqlBuilder = new StringBuilder(@"
            select id, title, account, pwd, remark, registerby, registerbyaccount
            from strongbox where id>1 ");

            var paraList = new List<SQLiteParameter>();

            if (box.ID > 0)
            {
                sqlBuilder.Append(" and id=@id");
                paraList.Add(new SQLiteParameter("@id", box.ID));
            }

            if (!string.IsNullOrWhiteSpace(box.Title))
            {
                sqlBuilder.Append(" and title=@title");
                paraList.Add(new SQLiteParameter("@title", box.Title));
            }

            if (!string.IsNullOrWhiteSpace(box.Account))
            {
                sqlBuilder.Append(" and account=@account");
                paraList.Add(new SQLiteParameter("@account", box.Account));
            }

            if (!string.IsNullOrWhiteSpace(box.Remark))
            {
                sqlBuilder.Append(" and remark like @remark");
                paraList.Add(new SQLiteParameter("@remark", box.Remark));
            }

            if (!string.IsNullOrWhiteSpace(box.RegisterBy))
            {
                sqlBuilder.Append(" and registerby=@registerby");
                paraList.Add(new SQLiteParameter("@registerby", box.RegisterBy));
            }

            if (!string.IsNullOrWhiteSpace(box.RegisterByAccount))
            {
                sqlBuilder.Append(" and registerbyaccount=@registerbyaccount");
                paraList.Add(new SQLiteParameter("@registerbyaccount", box.RegisterByAccount));
            }

            return SQLiteHelper.Query<StrongBox>(sqlBuilder.ToString(), paraList.ToArray());
        }

        public static int Edit(StrongBox box)
        {
            if (box.ID < 1)
                return -1;

            var sql = @"update strongbox set title=@title, account=@account, pwd=@pwd
                        , remark=@remark, registerby=@registerby, registerbyaccount=@registerbyaccount 
                        where id=@id";

            var paras = new SQLiteParameter[]
            {
                new SQLiteParameter("@id",box.ID),
                new SQLiteParameter("@title", box.Title),
                new SQLiteParameter("@account", box.Account),
                new SQLiteParameter("@pwd", box.Pwd),
                new SQLiteParameter("@remark", box.Remark),
                new SQLiteParameter("@registerby", box.RegisterBy),
                new SQLiteParameter("@registerbyaccount", box.RegisterByAccount)
            };

            return SQLiteHelper.ExecuteNonQuery(sql, paras);
        }

        public static int Del(long Id)
        {
            var sql = @"delete from strongbox where id=@id";
            var paras = new SQLiteParameter[] { new SQLiteParameter("@id", Id) };
            return SQLiteHelper.ExecuteNonQuery(sql, paras);
        }

        public static int Add(StrongBox box)
        {
            var sql = @"
                insert into strongbox (title, account, pwd, remark, registerby, registerbyaccount)
                values (@title, @account, @pwd, @remark, @registerby, @registerbyaccount)";
            var paras = new SQLiteParameter[]
            {
                new SQLiteParameter("@title",box.Title),
                new SQLiteParameter("@account",box.Account),
                new SQLiteParameter("@pwd",box.Pwd),
                new SQLiteParameter("@remark",box.Remark),
                new SQLiteParameter("@registerby",box.RegisterBy),
                new SQLiteParameter("@registerbyaccount",box.RegisterByAccount)
            };

            return SQLiteHelper.ExecuteNonQuery(sql, paras.ToArray());
        }
    }

    public static class StrongBoxExt
    {
        public static StrongBox ToDencrypt(this StrongBox box)
            => new StrongBox
            {
                ID = box.ID,
                Title = box.Title,
                Account = box.Account,
                Pwd = EncryptHelper.Decrypt(box.Pwd),
                Remark = box.Remark,
                RegisterBy = box.RegisterBy,
                RegisterByAccount = box.RegisterByAccount
            };

        public static IEnumerable<StrongBox> ToDencrypt(this IEnumerable<StrongBox> boxs)
            => boxs.Select(x => x.ToDencrypt());
    }
}
